package com.adingxiong.cft.quartz;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @ClassName DataJobDemo
 * @Description   定时任务之  数据传递
 * @Author xiongchao
 * @Date 2021/1/11 15:06
 **/
@Slf4j
public class DataJobDemo {

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        JobDetail jobDetail = JobBuilder.newJob(DataJob.class)
                .withIdentity("data","group01")
                .usingJobData("data","传递数据")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("data_group01")
                .startNow()
                .build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.scheduleJob(jobDetail,trigger);
        scheduler.start();

        //2s  关闭
        Thread.sleep(2_100);
        scheduler.shutdown();
    }


    public static class DataJob implements Job{
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            log.info("------------------------ dataJob ----------------------");
            String data = context.getJobDetail().getJobDataMap().getString("data");
            log.info("从定时任务中读取到的数据为：{}" ,data);
            log.info("------------------------ dataJob ----------------------");
        }
    }
}
